package com.linkoog.devtools.intellij.action;

import com.intellij.icons.AllIcons;
import com.intellij.ide.DataManager;
import com.intellij.ide.HelpTooltip;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionPopupMenu;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformCoreDataKeys;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.actionSystem.Toggleable;
import com.intellij.openapi.actionSystem.UpdateSession;
import com.intellij.openapi.actionSystem.ex.ActionButtonLook;
import com.intellij.openapi.actionSystem.ex.AnActionListener;
import com.intellij.openapi.actionSystem.ex.CustomComponentAction;
import com.intellij.openapi.actionSystem.impl.ActionButton;
import com.intellij.openapi.actionSystem.impl.ActionManagerImpl;
import com.intellij.openapi.actionSystem.impl.MenuItemPresentationFactory;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.ui.JBPopupMenu;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.scale.JBUIScale;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.messages.SimpleMessageBusConnection;
import com.intellij.util.ui.JBInsets;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.StartupUiUtil;
import com.linkoog.devtools.intellij.ui.button.SplitButton;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.geom.Area;
import java.util.Objects;

public class SplitButtonAction extends ActionGroup implements CustomComponentAction {
    private final ActionGroup myActionGroup;

    private final Icon myIcon;

    public SplitButtonAction(@NotNull ActionGroup actionGroup) {
        this(actionGroup,null);
    }

    public SplitButtonAction(@NotNull ActionGroup actionGroup, Icon icon) {
        myActionGroup = actionGroup;
        myIcon = icon;
        setPopup(true);
    }

    public @NotNull ActionGroup getActionGroup() {
        return myActionGroup;
    }

    @Override
    public @NotNull ActionUpdateThread getActionUpdateThread() {
        return myActionGroup.getActionUpdateThread();
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        Presentation presentation = e.getPresentation();
        SplitButton splitButton = ObjectUtils.tryCast(presentation.getClientProperty(CustomComponentAction.COMPONENT_KEY), SplitButton.class);

        myActionGroup.update(e);

        if (presentation.isVisible()) {
            AnAction action = splitButton != null ? splitButton.getSelectedAction() : getFirstEnabledAction(e);
            if (action != null) {
                Presentation actionPresentation = e.getUpdateSession().presentation(action);
                presentation.copyFrom(actionPresentation, splitButton);
                if (myIcon != null) presentation.setIcon(myIcon);
                if (splitButton != null) {
                    boolean shouldRepaint = splitButton.isActionEnabled() != presentation.isEnabled();
                    splitButton.setActionEnabled(presentation.isEnabled());
                    if (shouldRepaint) splitButton.repaint();
                }
                presentation.setEnabledAndVisible(true);
            }

            presentation.putClientProperty(SplitButton.FIRST_ACTION, splitButton != null ? null : action);
        }
    }

    @Nullable
    private AnAction getFirstEnabledAction(@NotNull AnActionEvent e) {
        UpdateSession session = e.getUpdateSession();
        var children = session.children(myActionGroup);
        var firstEnabled = ContainerUtil.find(children, a -> session.presentation(a).isEnabled());
        return firstEnabled != null ? firstEnabled : ContainerUtil.getFirstItem(children);
    }

    @Override
    public AnAction @NotNull [] getChildren(@Nullable AnActionEvent e) {
        return myActionGroup.getChildren(e);
    }

    @Override
    public boolean isDumbAware() {
        return myActionGroup.isDumbAware();
    }

    @Override
    @NotNull
    public JComponent createCustomComponent(@NotNull Presentation presentation, @NotNull String place) {
        return new SplitButton(this, presentation, place, myActionGroup, myIcon);
    }


}
